package Threa;

public class SaleTest implements Runnable {
    int   num=0;
    int count = 100;

   @Override
    public void run() {

        while(true){
           //同时进行抢票，进行就绪
            try{
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (this){//加一个锁 同步代码
                if (count<=0){//判断票数到零的时候退出
                    break;
                }
                num++;
                count--;
                try{
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+"抢到"+num+"张票，剩余"+count+"张票！");
            }
        }

    }
    public static void main(String[] args) {
        SaleTest t=new SaleTest();


        Thread sell1=new Thread(t);
        sell1.setName("陶");
        sell1.start();

        Thread sell2=new Thread(t);
        sell2.setName("黄");
        sell2.start();

        Thread sell3=new Thread(t);
        sell3.setName("代");
        sell3.start();
    }

}
